from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import api_view
from rest_framework.response import Response
from utils.base import BasePageNumberPagination
from utils.base_view import BaseModelViewSet
from apps.envs.models import (
    Env,
    EnvGlobalParams,
    EnvDb,
    EnvPlant,
    EnvService,
    Service,
    Plant,
    Module,
    Page,
    Headers
)
from apps.elements.models import Element
from apps.envs.serializers import (
    EnvSerializers,
    EnvGlobalParamsSerializers,
    HeadersSerializers,
    ServiceSerializers,
    EnvServiceSerializers,
    PlantSerializers,
    EnvPlantSerializers,
    ModuleSerializers,
    PageSerializers
)
from apps.envs.filters import (
    EnvFilter,
    EnvGlobalParamsFilter,
    HeadersFilter,
    ServiceFilter,
    EnvServiceFilter,
    PlantFilter,
    EnvPlantFilter,
    ModuleFilter,
    PageFilter
)


class EnvViewSet(BaseModelViewSet):
    serializer_class = EnvSerializers
    queryset = Env.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = EnvFilter


class EnvGlobalParamsSet(BaseModelViewSet):
    serializer_class = EnvGlobalParamsSerializers
    queryset = EnvGlobalParams.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = EnvGlobalParamsFilter


class ServiceViewSet(BaseModelViewSet):
    serializer_class = ServiceSerializers
    queryset = Service.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = ServiceFilter


class HeadersViewSet(BaseModelViewSet):
    serializer_class = HeadersSerializers
    queryset = Headers.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = HeadersFilter


class EnvServiceViewSet(BaseModelViewSet):
    serializer_class = EnvServiceSerializers
    queryset = EnvService.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = EnvServiceFilter


class PlantViewSet(BaseModelViewSet):
    serializer_class = PlantSerializers
    queryset = Plant.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = PlantFilter


class EnvPlantViewSet(BaseModelViewSet):
    serializer_class = EnvPlantSerializers
    queryset = EnvPlant.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = EnvPlantFilter


class ModuleViewSet(BaseModelViewSet):
    serializer_class = ModuleSerializers
    queryset = Module.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = ModuleFilter


class PageViewSet(BaseModelViewSet):
    serializer_class = PageSerializers
    queryset = Page.objects.all()
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = PageFilter


@api_view(['GET'])
def get_all_plant_module(request):
    project_id = request.query_params.get('project')
    all_plant = []
    plants = Plant.objects.all().filter(is_delete=False, project=project_id).values()
    for plant in plants:
        modules = Module.objects.all().filter(is_delete=False, plant=plant['id']).values()
        if modules:
            plant['children'] = modules
            all_plant.append(plant)
    return Response(all_plant)


@api_view(['GET'])
def get_all_plant_module_page(request):
    project_id = request.query_params.get('project')
    all_plant = []
    plants = Plant.objects.all().filter(is_delete=False, project=project_id).values()
    for plant in plants:
        has_page = False
        modules = Module.objects.all().filter(is_delete=False, plant=plant['id']).values()
        all_module = []
        for module in modules:
            pages = Page.objects.all().filter(is_delete=False, module=module['id']).values()
            if pages:
                module['children'] = pages
                has_page = True
                all_module.append(module)
        if has_page:
            plant['children'] = all_module
            all_plant.append(plant)

    return Response(all_plant)


@api_view(['GET'])
def get_all_plant_element(request):
    project_id = request.query_params.get('project')
    all_plant = []
    plants = Plant.objects.all().filter(is_delete=False, project=project_id).values()
    for plant in plants:
        has_element = False
        modules = Module.objects.all().filter(is_delete=False, plant=plant['id']).values()
        all_module = []
        for module in modules:
            pages = Page.objects.all().filter(is_delete=False, module=module['id']).values()
            all_page = []
            for page in pages:
                elements = Element.objects.all().filter(is_delete=False, page=page['id']).values()
                if elements:
                    page['children'] = elements
                    has_element = True
                all_page.append(page)
            if has_element:
                module['children'] = all_page
                all_module.append(module)
        if has_element:
            plant['children'] = all_module
            all_plant.append(plant)

    return Response(all_plant)
